#include "stack.hpp"
#include <cassert>

namespace stack
{
	void push(Stack &stack, StackElement &newElement)
	{
		newElement.next = stack.top;
		stack.top       = &newElement;

		stack.size++;
	}

	StackElement& pop(Stack &stack)
	{
		assert(stack.size > 0);

		StackElement *oldElement = stack.top;
		stack.top                = stack.top->next;
		stack.size--;

		return *oldElement;
	}

	/*
	 * Helferfunktion für die Funktion toString() um die Elemente
	 * in stack wieder richtig einordnen zu können.
	 */
	static Stack reverse(Stack &stack)
	{
		Stack newStack = {0, 0};

		while(stack.size != 0)
		{
			push(newStack, pop(stack));
		}

		return newStack;
	}


	const string toString(Stack &stack)
	{
		if(stack.size == 0)
		{
			return "--";
		}

		Stack  tmpStack = { 0, 0 };
		string tmpString = "";

		do
		{
			StackElement &tmpElement = pop(stack);

			push(tmpStack, tmpElement);
			tmpString += tmpElement.content;
			tmpString += " <-- ";
		}
		while(stack.size != 0);

		stack = reverse(tmpStack);

		return tmpString;
	}
}
